package kyuu.acshmily.comment
{
	import flash.filters.GlowFilter;
	
	import mx.controls.Label;
	import mx.effects.Effect;
	import mx.effects.Fade;
	import mx.effects.Iris;
	import mx.effects.WipeDown;
	import mx.effects.WipeLeft;
	import mx.effects.WipeRight;
	import mx.effects.WipeUp;

	/**
	 * 弹幕组件类
	 */
	public class CommentLabel extends Label
	{
		// 显示效果和消失效果
		private var _showEff:Effect = null;
		private var _hideEff:Effect = null;

		// 效果是否被暂停
		private var _isShowEffectPaused:Boolean = false;
		private var _isHideEffectPaused:Boolean = false;

		/**
		 * 逻辑坐标：
		 * 根据碰撞检测计算出来的逻辑位置坐标，非实际坐标。
		 * 实际坐标是逻辑坐标对舞台的大小取模之后的结果。
		 */
		public var logicX:Number = 0;
		public var logicY:Number = 0;

		/**
		 * 该弹幕是否被锁定
		 */
		public var isLocked:Boolean = false;

		public function CommentLabel(){
			super();

			// 初始化样式
			setStyle("fontFamily", "黑体");
			setStyle("fontWeight", "bold");
			filters = new Array(new GlowFilter(0x000000,1,8,8,1));
			//filters = new Array(new DropShadowFilter(2,45,0xCCCCCC,0.7,0,0));
			selectable = false;
			visible = false;
		}

		/**
		 * 设置弹幕信息
		 * @param info - 描述弹幕信息的XML
		 */
		public function setComment(info:XML):void{
			// 设置显示时的特效
			_showEff = createEffect(info.@showEffect);
			setStyle("showEffect", _showEff);
			// 设置消失时的特效
			_hideEff = createEffect(info.@hideEffect);
			setStyle("hideEffect", _hideEff);

			// 设置是否被锁定
			isLocked = (info.@isLocked > 0);

			// 设置字体信息和文字
			setStyle("fontSize", info.@fontSize);
			setStyle("color", info.@color);
			text = info.message;
			validateNow();

			if(info.@hasBorder && info.@hasBorder>0){
				this.textField.border = true;
				this.textField.borderColor = 0x0080ff;
			}

			// 设置宽度和高度
			width = textWidth * 1.1;
			height = textHeight * 1.1;
		}

		/**
		 * 根据效果ID，实例化对应的效果
		 * @param effID - 效果ID
		 * @return 对应的效果实例
		 */
		private function createEffect(effID:int):Effect{
			if(isNaN(effID) || effID <=0 ) return null;
			var eff:Effect = null;
			switch(effID){
				case 1:
					eff = new Fade(this);
					break;
				case 2:
					eff = new Iris(this);
					break;
				case 3:
					eff = new WipeLeft(this);
					break;
				case 4:
					eff = new WipeRight(this);
					break;
				case 5:
					eff = new WipeUp(this);
					break;
				case 6:
					eff = new WipeDown(this);
					break;
				default:
					eff = null;
					break;
			}
			if(eff != null) eff.duration = 1000;
			return eff;
		}

		/**
		 * 设置标签的显示/隐藏，但是不调用对应的效果
		 * 用于当过滤条件改变时，隐藏已经存在的弹幕的
		 * @param show - 是否显示弹幕
		 */
		public function showWithoutEffect(show:Boolean):void{
			// 清除特效
			setStyle("showEffect", null);
			setStyle("hideEffect", null);

			visible = show;

			// 恢复特效
			setStyle("showEffect", _showEff);
			setStyle("hideEffect", _hideEff);
		}

		/**
		 * 检查弹幕的显示特效
		 * @return 是否存在
		 */
		public function hasShowEffect():Boolean{
			return (_showEff != null);
		}

		/**
		 * 检查弹幕的隐藏特效
		 * @return 是否存在
		 */
		public function hasHideEffect():Boolean{
			return (_hideEff != null);
		}

		/**
		 * 通知弹幕停止正在播放的效果
		 */
		public function notifyPause():void{
			if(_hideEff != null && _hideEff.isPlaying){
				_hideEff.pause();
				_isHideEffectPaused = true;
			}
			if(_showEff != null && _showEff.isPlaying){
				_showEff.pause();
				_isShowEffectPaused = true;
			}
		}

		/**
		 * 通知弹幕恢复被暂停的效果
		 */
		public function notifyResume():void{
			if(_hideEff != null && _isHideEffectPaused){
				_hideEff.resume();
				_isHideEffectPaused = false;
			}
			if(_showEff != null && _isShowEffectPaused){
				_showEff.resume();
				_isShowEffectPaused = false;
			}
		}
	}
}